<?php
namespace Admin\Model;

use Think\Model;

/**
 * 内容模型
 */
class TagModel extends Model
{

    public function addContent()
    {
        header("Content-type:text/html;charset=utf-8");
        //自动验证
        $this->_validate = array(
            array('tname', 'require', 'tag标题不能为空')
        );
        if (!$this->create()) {
            return false;
        }
        $data = $_POST;
        $data['tname'] = str_replace(array(',', '，', '.', '。', '|', '\\', '/', "\r", '\s', "\n", ' ', "\r\n"), ',', $data['tname']);
        $data['tname'] = array_filter(explode(',', trim($data['tname'])));
        //检查标签重复性
        $oldData = M('tag')->field('tname')->select();
        foreach ($oldData as $k => $v) {
            $tname[] = $v['tname'];
        }
        foreach ($data['tname'] as $k => $v) {
            if (in_array($v, $tname)) {
                $this->error = '标签【 ' . $v . ' 】已存在不能重复添加,请重新操作';
                return false;
            }
        }
        foreach ($data['tname'] as $k => $v) {
            $addData['tname'] = $v;
            M('tag')->add($addData);
        }

        return true;
    }

    /**
     * 修改内容
     */
    public function editContent()
    {
        header("Content-type:text/html;charset=utf-8");
        $aid = I('get.tid');
        //自动验证
        $this->_validate = array(
            array('tname', 'require', 'tag标题不能为空')
        );
        if (!$this->create()) {
            return false;
        }
        $data = $_POST;
        M('tag')->where("tid='{$id}'")->save($data);
        return true;
    }

    /**
     * 删除文章
     */
    public function delContent()
    {
        $tid = I("get.tid");
        M('tag')->where("tid='{$tid}'")->delete();
        return true;
    }

}
